<!--社区列表-->
<template>
    <div>
        <el-card class="box-card" shadow="never">
          <el-breadcrumb separator-class="el-icon-arrow-right">
              <el-breadcrumb-item>社区管理</el-breadcrumb-item>
              <el-breadcrumb-item>社区列表</el-breadcrumb-item>
          </el-breadcrumb>
            <div class="yx_line"></div>
            <el-row :gutter="20" class="yx_el_row">
                <el-col :span="4">
                    <el-button type="primary" plain @click="add">添加</el-button>
                </el-col>
            </el-row>
            <el-table class="yx_table" border :data="communitylist">
                <el-table-column prop="index" label="序号" width="100">
                </el-table-column>
                <el-table-column prop="c_name" label="社区名称" width="180">
                </el-table-column>
                <el-table-column prop="phone" label="电话" width="180">
                </el-table-column>
                <el-table-column prop="address" label="地址" width="100">
                </el-table-column>
                <el-table-column prop="sort" label="排序" width="180">
                </el-table-column>
                <el-table-column prop="enabled" label="状态" width="100">
                </el-table-column>
                <el-table-column  label="操作">
                    <template slot-scope="scope">
                        <el-tooltip class="item" effect="dark" content="编辑" placement="bottom-start">
                            <el-button type="primary" icon="el-icon-edit" @click="edit(scope.row.id,2)" circle></el-button>
                        </el-tooltip>
                        <el-tooltip class="item" effect="dark" content="查看" placement="bottom-start">
                            <el-button type="info" icon="el-icon-notebook-1" @click="edit(scope.row.id,0)" circle></el-button>
                        </el-tooltip>
                        <el-tooltip class="item" effect="dark" content="删除" placement="bottom-start">
                            <el-button type="warning" icon="el-icon-delete" @click="del(scope.row.id)" circle></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination class="yx_page"
                           background
                           @current-change="handle_current_change"
                           @size-change="handle_size_change"
                           :page-sizes="page_sizes"
                           :page-size="page_size"
                           layout="prev, pager, next,sizes, total"
                           :total="total_count">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: 'CommunityList',
        inject:['reload'],
        data() {
            return {
                current_page:1,
                page_size:20
            }
        },
        methods: {
            getList(){
                var send_data={
                    api_token:this.$store.getters.getSessionId,
                    current_page:this.current_page,
                    page_size:this.page_size
                }
                this.$store.dispatch('loadCommunityList',send_data);
            },
            add(){
                this.$router.push({
                    name:'communityedit',
                    params:{id:-1,edit_type:1}
                });
            },
            edit(id,edit_type){
                this.$router.push({
                    name:'communityedit',
                    params:{id:id,edit_type:edit_type}
                });
            },
            del(id){
                let that=this;
                //弹框提示
                this.$confirm('确认删除, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then((e) => {
                    var send_data={
                        api_token : that.$store.getters.getSessionId,
                        id:id
                    };
                that.$store.dispatch('delCommunity',send_data);
                })
            },
            //跳转页
            handle_current_change(current_page){
                //当前页码数
                this.current_page=current_page;
                this.getList();
            },
            //修改条数
            handle_size_change(page_size){
                this.current_page=1;
                this.page_size=page_size;
                this.getList();
            }
        },
        created() {
            //加载列表
            this.getList();
        },
        computed:{
            communitylist(){
                return this.$store.getters.getCommunityList;
            },
            total_count(){
                return this.$store.getters.getCommunityTotalCount;
            },
            page_sizes(){
                return this.$store.state.page_sizes;
            },
            delCommunityStatus(){
                return this.$store.getters.getDelCommunityStatus;
            }
        },
        watch:{
            delCommunityStatus(val){
                let that=this;
                if(val==1){
                    this.$message({
                        message:'删除成功',
                        type:'success',
                        duration:1000,
                        onClose:function(e){
                            that.$store.commit('setDelCommunityStatus',-1);
                            that.reload();
                        }
                    });
                }else if(val==0){
                    this.$message({
                        message:'删除失败',
                        type:'error',
                        duration:2000,
                        onClose:function(e){
                            that.reload();
                        }
                    });
                    //初始化状态
                    that.$store.commit('setDelCommunityStatus',-1);
                }
            }
        }
    }
</script>

<style>
     @import url('../../assets/css/table.css');
</style>
